import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/pctask/PcrwViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpBaseConst.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/StringUtils.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:flutter_ahj_app/base/widget/NoneData.dart';


class PCrwStartDetailView extends BaseStatefulWidget {
  final String hazardInvestigationRiskPointId;
  PCrwStartDetailView({this.hazardInvestigationRiskPointId});

  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    // throw UnimplementedError();
    return PCrwStartDetailViewState();
  }
}

class PCrwStartDetailViewState extends BaseState<PcrwViewModel, PCrwStartDetailView> {
  ScrollController _scrollController;
  List _datalist; //请求的数据列表
  int pageIndex = HttpBaseConst.pageIndex;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  @override
  Widget initWidget() {
    return Scaffold(
      backgroundColor: Color(0xfff4f5f6),
      appBar: MTopBar(
        elevation: 0,
        title: '隐患排查',
      ).create(),
      body: _bodyView(),
    );
  }


  _bodyView() {
    return Column(
      children: [
        Expanded(
            child: Container(
              margin: EdgeInsets.fromLTRB(
                  Dimens.dimens16, Dimens.dimens16, Dimens.dimens16, Dimens.dimens16),
              child: _datalist == null
                  ? LoadingWidget()
                  : _datalist.length == 0
                  ? NoneData()
                  : ListView.builder(
                itemCount: _datalist.length,
                controller: _scrollController,
                physics: AlwaysScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  return _item(index);
                },
              ),
            )),
      ],
    );
  }

  _item(int index) {
    return Container(
      child: Column(
        children: [
          Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              decoration: BoxDecoration(
                color: SQColor.white,
                borderRadius: BorderRadius.circular(6),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Stack(
                        alignment: Alignment.center,
                        children: [
                          Image.asset(
                              "img/pxrw_zt_d.png",
                              width: 55,
                              height: Dimens.dimens20,
                              fit: BoxFit.fill),
                          JText(
                            StringUtils.getStr(_datalist[index]["hazardInvestigationContentTypeName"]),
                            color: SQColor.white,
                            fontSize: Dimens.dimens12,
                          ),
                        ],
                      ),
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 5),
                    child: JText(
                      StringUtils.getStr(_datalist[index]["dangerSourceName"]),
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.fromLTRB(Dimens.dimens12, Dimens.dimens12, Dimens.dimens12, Dimens.dimens12),
                    padding: EdgeInsets.all(Dimens.dimens12),
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Color(0xffF5F7F9),
                      borderRadius: BorderRadius.circular(6.0),
                    ),
                    child: Container(
                      child:
                      JText(
                        StringUtils.getStr(_datalist[index]["controlMeasure"]),
                      ),
                    ),),
                  SizedBox(
                    height: Dimens.dimens16,
                  ),
                  _bottomView(index),
                  SizedBox(
                    height: Dimens.dimens16,
                  ),
                ],
              )),
          Container(
            height: 10,
            color: Color(0xfff6f6f6),
            width: double.infinity,
          ),
        ],
      ),
    );
  }

  _bottomView(int index) {
    String _typeName = StringUtils.getStr(_datalist[index]["hazardInvestigationContentTypeName"]);
    return _typeName == "未排查" ? _ywc(index) : _typeName == "正常" ? _ywczc(index) : _yborzd(index);
  }

  _ywc(int index) {
    return Container(
      padding: EdgeInsets.only(left: 5),
      child: Row(
        children: [
          Expanded(
            child: InkWell(
              onTap: () {
                _yhitemClick('正常', index);

              },
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(right: 5),
                width: 88,
                height: 35,
                decoration: BoxDecoration(
                    color: SQColor.white,
                    border: Border.all(
                      color: Color(0xff165DFF),
                    ),
                    borderRadius: BorderRadius.circular(16)),
                child: JText('正常', color: Color(0xff165DFF)),
              ),
            ),
          ),
          Expanded(
            child: InkWell(
              onTap: () {
                _yhitemClick('登记隐患', index);
              },
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(right: 5),
                width: 88,
                height: 35,
                decoration: BoxDecoration(
                    color: Color(0xff165DFF),
                    borderRadius: BorderRadius.circular(16)),
                child: JText('登记隐患', color: Colors.white),
              ),
            ),
          ),
        ],
      ),
    );
  }
  //正常排查
  _ywczc(int index) {
    return Container(
      padding: EdgeInsets.only(right: 12),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          InkWell(
            onTap: () {
              _yhitemClick('撤回', index);
            },
            child: Container(
              alignment: Alignment.center,
              margin: EdgeInsets.only(right: 5),
              width: 88,
              height: 35,
              decoration: BoxDecoration(
                  color: SQColor.white,
                  border: Border.all(
                    color: Color(0xff165DFF),
                  ),
                  borderRadius: BorderRadius.circular(16)),
              child: JText('撤回', color: Color(0xff165DFF)),
            ),
          ),
        ],
      ),
    );
  }


//一般或重大隐患
  _yborzd(int index) {
    return Container(
      padding: EdgeInsets.only(left: 5),
      child: Row(
        children: [
          Expanded(
            child: InkWell(
              onTap: () {
                _yhitemClick('撤回', index);
              },
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(right: 5),
                width: 88,
                height: 35,
                decoration: BoxDecoration(
                    color: SQColor.white,
                    border: Border.all(
                      color: Color(0xff165DFF),
                    ),
                    borderRadius: BorderRadius.circular(16)),
                child: JText('撤回', color: Color(0xff165DFF)),
              ),
            ),
          ),
          Expanded(
            child: InkWell(
              onTap: () {
                _yhitemClick('查看隐患', index);
              },
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(right: 5),
                width: 88,
                height: 35,
                decoration: BoxDecoration(
                    color: Color(0xff165DFF),
                    borderRadius: BorderRadius.circular(16)),
                child: JText('查看隐患', color: Colors.white),
              ),
            ),
          ),
        ],
      ),
    );
  }


  _yhitemClick(String title, int index) {
    switch (title) {
      case '登记隐患':
        Navigator.pushNamed(context, '/PCrwYhdjPageView', arguments: _datalist[index]).then((value) {
          setState(() {
            _datalist.clear();
            _getData();
          });
        });
        break;
      case '正常':
        _getUpdatenormal(index);
        break;
      case '撤回':
        _getUpdateBack(index);
        break;
      case '查看隐患':
        Map _m = {
          'hazardInvestigationTaskId' : StringUtils.getStr(_datalist[index]['hazardInvestigationTaskId'].toString()),
          'hazardInvestigationRiskPointId' : StringUtils.getStr(_datalist[index]['hazardInvestigationRiskPointId'].toString()),
          'hazardInvestigationContentId' : StringUtils.getStr(_datalist[index]['hazardInvestigationContentId'].toString()),
        };
        print(_m);
        Navigator.pushNamed(context, '/PcrwyhDetailView', arguments: _m);
        break;
      default:
    }
  }

  //正常
  _getUpdatenormal(int index) async {
    var map = {
      'hazardInvestigationTaskId': StringUtils.getStr(_datalist[index]['hazardInvestigationTaskId'].toString()),
      'hazardInvestigationRiskPointId': StringUtils.getStr(_datalist[index]['hazardInvestigationRiskPointId'].toString()),
      'hazardInvestigationContentId': StringUtils.getStr(_datalist[index]['hazardInvestigationContentId'].toString()),
      'hazardInvestigationContentType': 'normal'
    };
    viewModel.getHazardUpdate(map);
    viewModel.getHazardUpdateStream().listen((result) {
      if (result['code'] == "0") {
        _datalist.clear();
        _getData();
      } else {
        ToastUtils.showToast(result['msg']);
      }
    });
  }

  //撤回
  _getUpdateBack(int index) async {
    var map = {
      'hazardInvestigationTaskId': StringUtils.getStr(_datalist[index]['hazardInvestigationTaskId'].toString()),
      'hazardInvestigationRiskPointId': StringUtils.getStr(_datalist[index]['hazardInvestigationRiskPointId'].toString()),
      'hazardInvestigationContentId': StringUtils.getStr(_datalist[index]['hazardInvestigationContentId'].toString()),
    };
    viewModel.getHazardUpdate1(map);
    viewModel.getHazardUpdateStream1().listen((result) {
      if (result['code'] == "0") {
        _datalist.clear();
        _getData();
      } else {
        ToastUtils.showToast(result['msg']);
      }
    });
  }

  ///获得数据
  _getData() async {
    var map = {
      'hazardInvestigationRiskPointId': StringUtils.getStr(widget.hazardInvestigationRiskPointId)
    };
    viewModel.getpcDetail(map);
    viewModel.getPcDetailStream().listen((result) {
      if (result['code'] == "0") {
        if (_datalist == null) _datalist = List();
        if (result == null ||
            result['data'] == null  ||
            result['data'].length == 0) {
          setState(() {

          });
        } else {
          setState(() {
            _datalist.addAll(result['data']);
          });
        }
        setState(() {

        });
        print("******************length = ${_datalist.length}");
      } else {
        setState(() {
          _datalist = List();
        });
        ToastUtils.showToast(result['msg']);
      }
    });
  }

}
